/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

/**
 * 
 */
package edu.ucla.cs.typecast.test;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.util.Date;

import edu.ucla.cs.typecast.net.EndPoint;

/**
 *
 * @date Dec 1, 2007
 */
public class PingServerExt extends PingServer {
	private PrintStream logStream; 
	private Date startDate;
	
	public PingServerExt(Class targetType, EndPoint endPoint, long delay, long duration, OutputStream ostream) throws IOException { 
		super(targetType, endPoint, delay, duration, ostream); 
		logStream = new PrintStream(ostream); 
	}
	
	protected void init() { 
		super.init(); 
		startDate = new Date(); 
		logStream.println("============================================="); 
		logStream.println("Export service object at time  " + startDate.getTime()); 		
//		logStream.println("0.0.0.0" + " " + "0" + " " + 0 + " " + startDate.getTime()); 
	}

	public void ping(InetAddress source, EndPoint returnAddr, String sessionID, long seqno, 
			byte[] payload) {
		Date now = new Date(); 
		logStream.println(source.getHostAddress() + " " + sessionID + " " + seqno + " " + (now.getTime() - startDate.getTime())); 
		logStream.flush(); 		
		super.ping(source, returnAddr, sessionID, seqno, payload); 
	}
	
	public void saveStatistics(PrintStream out) { 
		logStream.println("============================================="); 
		logStream.flush(); 
		super.saveStatistics(out); 
	}
}
